package com.buaa.divinationmaster;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    private TextView titleText;
    private CardView cardTarot, cardMeihua, cardCoin, cardLenormand;
    private LinearLayout btnHistory, btnSettings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        
        initViews();
        setupAnimations();
        setupClickListeners();
    }
    
    private void initViews() {
        titleText = findViewById(R.id.titleText);
        cardTarot = findViewById(R.id.cardTarot);
        cardMeihua = findViewById(R.id.cardMeihua);
        cardCoin = findViewById(R.id.cardCoin);
        cardLenormand = findViewById(R.id.cardLenormand);
        btnHistory = findViewById(R.id.btnHistory);
        btnSettings = findViewById(R.id.btnSettings);
    }
    
    private void setupAnimations() {
        // 标题脉冲动画
        ObjectAnimator pulseAnimator = ObjectAnimator.ofFloat(titleText, "alpha", 1.0f, 0.8f, 1.0f);
        pulseAnimator.setDuration(3000);
        pulseAnimator.setRepeatCount(ObjectAnimator.INFINITE);
        pulseAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        pulseAnimator.start();
        
        // 2x2网格卡片入场动画 - 对角线效果
        animateCardEntrance(cardTarot, 0);      // 左上
        animateCardEntrance(cardMeihua, 150);   // 右上  
        animateCardEntrance(cardCoin, 300);     // 左下
        animateCardEntrance(cardLenormand, 450); // 右下
        
        // 底部菜单栏入场动画
        animateMenuEntrance();
    }
    
    private void animateCardEntrance(View card, long delay) {
        card.setAlpha(0f);
        card.setTranslationY(100f);
        
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(card, "alpha", 0f, 1f);
        ObjectAnimator translateAnimator = ObjectAnimator.ofFloat(card, "translationY", 100f, 0f);
        
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(alphaAnimator, translateAnimator);
        animatorSet.setDuration(500);
        animatorSet.setStartDelay(delay);
        animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
        animatorSet.start();
    }
    
    private void animateMenuEntrance() {
        LinearLayout menuLayout = findViewById(R.id.menuLayout);
        menuLayout.setAlpha(0f);
        menuLayout.setTranslationY(50f);
        
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(menuLayout, "alpha", 0f, 1f);
        ObjectAnimator translateAnimator = ObjectAnimator.ofFloat(menuLayout, "translationY", 50f, 0f);
        
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(alphaAnimator, translateAnimator);
        animatorSet.setDuration(600);
        animatorSet.setStartDelay(600);
        animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
        animatorSet.start();
    }
    
    private void setupClickListeners() {
        cardTarot.setOnClickListener(v -> {
            animateCardClick(v);
            startActivity(new Intent(MainActivity.this, TarotActivity.class));
        });
        
        cardMeihua.setOnClickListener(v -> {
            animateCardClick(v);
            startActivity(new Intent(MainActivity.this, MeihuaActivity.class));
        });
        
        cardCoin.setOnClickListener(v -> {
            animateCardClick(v);
            startActivity(new Intent(MainActivity.this, CoinActivity.class));
        });
        
        cardLenormand.setOnClickListener(v -> {
            animateCardClick(v);
            startActivity(new Intent(MainActivity.this, LenormandActivity.class));
        });
        
        // 底部菜单按钮功能 - LinearLayout点击事件
        btnHistory.setOnClickListener(v -> {
            animateMenuClick(v);
            startActivity(new Intent(MainActivity.this, HistoryActivity.class));
        });
        
        btnSettings.setOnClickListener(v -> {
            animateMenuClick(v);
            startActivity(new Intent(MainActivity.this, SettingsActivity.class));
        });
    }
    
    private void animateCardClick(View card) {
        ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(card, "scaleX", 1.0f, 0.95f);
        ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(card, "scaleY", 1.0f, 0.95f);
        ObjectAnimator scaleUpX = ObjectAnimator.ofFloat(card, "scaleX", 0.95f, 1.0f);
        ObjectAnimator scaleUpY = ObjectAnimator.ofFloat(card, "scaleY", 0.95f, 1.0f);
        
        AnimatorSet scaleDown = new AnimatorSet();
        scaleDown.playTogether(scaleDownX, scaleDownY);
        scaleDown.setDuration(100);
        
        AnimatorSet scaleUp = new AnimatorSet();
        scaleUp.playTogether(scaleUpX, scaleUpY);
        scaleUp.setDuration(100);
        
        AnimatorSet clickAnimation = new AnimatorSet();
        clickAnimation.playSequentially(scaleDown, scaleUp);
        clickAnimation.start();
    }
    
    private void animateMenuClick(View menuItem) {
        ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(menuItem, "scaleX", 1.0f, 0.9f);
        ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(menuItem, "scaleY", 1.0f, 0.9f);
        ObjectAnimator scaleUpX = ObjectAnimator.ofFloat(menuItem, "scaleX", 0.9f, 1.0f);
        ObjectAnimator scaleUpY = ObjectAnimator.ofFloat(menuItem, "scaleY", 0.9f, 1.0f);
        
        AnimatorSet scaleDown = new AnimatorSet();
        scaleDown.playTogether(scaleDownX, scaleDownY);
        scaleDown.setDuration(80);
        
        AnimatorSet scaleUp = new AnimatorSet();
        scaleUp.playTogether(scaleUpX, scaleUpY);
        scaleUp.setDuration(80);
        
        AnimatorSet clickAnimation = new AnimatorSet();
        clickAnimation.playSequentially(scaleDown, scaleUp);
        clickAnimation.start();
    }
    
    private void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}